package com.zhangjianling.premp.controller.sys;

import com.zhangjianling.premp.controller.base.BaseController;
import com.zhangjianling.premp.entity.sys.ResultEntity;
import com.zhangjianling.premp.entity.sys.SysUser;
import com.zhangjianling.premp.server.sys.SysUserService;
import com.zhangjianling.premp.utils.SystemAopLog;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * 用户登录管理
 * @author zhangjl
 *
 */
@Controller
public class LoginController extends BaseController {
	@Resource
	private SysUserService sysUserService;
	/**
	 * 登录
	 * @return
	 */
	@PostMapping("sysLogin")
	@SystemAopLog(description="用户登录")
	@ResponseBody
	public ResultEntity login(SysUser user, HttpServletRequest req, String kaptchaCode ){
		ResultEntity res = new ResultEntity();
		Session session = null;
		Subject currentUser = null;
		try{
			String kaptchaExpected = (String)req.getSession().getAttribute("rightCode");
			if(StringUtils.isNotBlank("kaptchaExpected") && !kaptchaExpected.equalsIgnoreCase(kaptchaCode)){
				res.setMessage("验证码错误");
				res.setCode(ERROR);
			}else{
				if(user !=null){
					currentUser = SecurityUtils.getSubject();
					SecurityUtils.getSubject().getSession().setTimeout(360000);
					System.out.println(currentUser.getSession().getTimeout());

					UsernamePasswordToken token  = new UsernamePasswordToken(user.getUserName(), user.getPassword());
					currentUser.login(token);
					session=currentUser.getSession();
					if(session!=null){
						res.setMessage("登录成功");
						res.setCode(SUCCESS);
					}else{
						res.setMessage("登录失败");
						res.setCode(ERROR);
					}
				}else{
					res.setMessage("登录失败");
					res.setCode(ERROR);
				}
			}
			
		}catch(LockedAccountException e){
			e.printStackTrace();
			res.setMessage("此账号已锁定，请联系管理员。");
			res.setCode(ERROR);
		}catch(IncorrectCredentialsException e){
			e.printStackTrace();
			res.setMessage("用户名或密码错误");
			res.setCode(ERROR);
		}catch(Exception e){
			e.printStackTrace();
			res.setMessage("登录失败，请联系管理员。");
			res.setCode(ERROR);
		}
		return res;
	}
	
	@GetMapping("unauthorized")
	public String unauthorized(){
		return "unauthorized";
	}
	
	/**
	 * 退出登录
	 * @return
	 */
	@GetMapping("logout")
	@SystemAopLog(description = "退出系统")
	@ResponseBody
	public ResultEntity logout(){
		ResultEntity res = new ResultEntity();
		try{
			Subject currentUser = SecurityUtils.getSubject();
			currentUser.logout();
			res.setMessage(OPERATE_SUCCESS);
			res.setCode(SUCCESS);
		}catch(Exception e){
			res.setMessage(OPERATE_ERROR);
			res.setCode(ERROR);
		}
		return res;
	}
	
	@GetMapping("login")
	public String login(){
		return "login";
	}
	
	/**
	 * 登录成功进入主页
	 * @return
	 */
	@GetMapping("sysIndex")
	public String index(){
		Subject subject = SecurityUtils.getSubject();
		try {
			SysUser user = (SysUser) subject.getPrincipal();
			if (user != null) {
				return "sys/index";
			}else{
				return "redirect:login";
			}
		}catch(Exception e){
			return "sys/index";
		}
	}
	
	/**
	 * 
	 * @return
	 */
	@GetMapping("getUserInfo")
	@ResponseBody
	public ResultEntity getUser(){
		ResultEntity res = new ResultEntity();
		try{
			Subject subject = SecurityUtils.getSubject();
			SysUser user = (SysUser)subject.getPrincipal();
			SysUser sysUser = sysUserService.findByUserName(user.getUserName());
			res.setData(sysUser);
			res.setMessage(OPERATE_SUCCESS);
			res.setCode(SUCCESS);
		}catch(Exception e){
			e.printStackTrace();
			res.setMessage(OPERATE_ERROR);
			res.setCode(ERROR);
		}
		return res;
	}
}
